var t = require("../../components/wux"), e = function(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}(require("../../asset/plugins/WxValidate"));
var api = require("../../api.js"),
  app = getApp();
var r;
Page({
    data: {
        tel: "",
        agree:false
    },
    onLoad: function() {
        this.initValidate();
        r = this;
    },
    onShow:function(){
        var that = this;
        r.toast = that.selectComponent("#toast");
       }, 
    showToptips: function(e) {
        t.$wuxToptips.show({
            timer: 3e3,
            text: e.msg || "请填写正确的字段",
            success: function() {
                return console.log("toptips", e);
            }
        });
    },
    userNameTel: function(t) {
        this.setData({
            tel: t.detail.value
        });
    },
    submitForm: function(t) {
        var e = t.detail.value;
        if (console.log(e), !this.WxValidate.checkForm(t)) {
            var o = this.WxValidate.errorList[0];
            return this.showToptips(o), !1;
        }
        if(!this.data.agree)  return e.msg = "请选择同意使用协议", void this.showToptips(e);
        var n = new Object();
        var user = getApp().globalData.wxUserInfo;
        // n.phone = e.tel, n.openId = user.openId,
        n.phone = e.tel,n.code=e.code;
        console.log(n)
        wx.request({
            url:api.login.BindPhoneLogin ,
              data:n,
              method: "POST",
              header: {
                  "content-type": "application/x-www-form-urlencoded"
              },
            success: function(res) {
              if (res.statusCode == 200&&res.data.code==200) {
                  if(res.data.code==200){
                    wx.showToast({
                        title: '绑定成功'
                      })
                      setTimeout(function(){
                        var pages = getCurrentPages(); //获取页面栈
                      if (pages.length > 1) {
                        //上一个页面实例对象
                        var prePage = pages[pages.length - 2];
                        //调用上一个页面的onShow方法
                        prePage.onLoad();
                        wx.navigateBack({
                          delta: 1
                        })
                      }
                      },2000)
                  }else{
                      r.toast.showToast(res.data.msg);
   
                  }
           
              }
            }
          });
    },
    vcode: function() {
        var e = {}, o = this;
        if ("" == this.data.tel) return e.msg = "请输入手机号码", void this.showToptips(e);
        if (this.data.tel.length < 11) return e.msg = "请输入正确的手机号", void this.showToptips(e);
        var n = new Object();
        var user = getApp().globalData.wxUserInfo;
        n.phone = this.data.tel, n.openId = user.openId,
        wx.request({
            url:api.login.checkBindPhone ,
            data:{openId:user.openId},
            success:function(data){
               if(data.statusCode==200&&data.data.code==200){
                   if(data.data.data== 0){
                        wx.request({
                            url:api.login.phoneCode ,
                            data:n,
                            method: "POST",
                            header: {
                                "content-type": "application/x-www-form-urlencoded"
                            },
                            success: function(res) {
                            if(res.statusCode=200){
                                if (o.c2 && o.c2.interval) return !1;
                                o.c2 = new t.$wuxCountDown({
                                    date: +new Date() + 6e4,
                                    onEnd: function() {
                                        this.setData({
                                            c2: "重新获取验证码"
                                        });
                                    },
                                    render: function(t) {
                                        var e = this.leadingZeros(t.sec, 2) + " 秒 ";
                                        0 !== t.sec && o.setData({
                                            c2: e
                                        });
                                    }
                                })
                            }
                            }, function() {
                                    e.msg = "获取验证码失败", o.showToptips(e);    
                            }
                        });
                   }else{
                    e.msg = data.data.msg, o.showToptips(e);
                   }
               }else{
                e.msg = data.data.msg, o.showToptips(e);
               }

            }
        })

    },
    initValidate: function() {
        var t = this;
        this.WxValidate = new e.default({
            tel: {
                required: !0,
                tel: !0
            },
            code: {
                required: !0,
                code: !0
            }
        }, {
            tel: {
                required: "请输入手机号",
                tel: "请输入正确的手机号"
            },
            code: {
                required: "请输入验证码"
            }
        }), this.WxValidate.addMethod("code", function(e, i) {
            return t.WxValidate.optional(e) || 6 == e.length;
        }, "请输入6位数的验证码");
    },
    agree:function(e){
        var that = this;
        console.log(e)
        var type = e.currentTarget.dataset.type;
        if(type==2){
         that.setData({
          agree:true
         })
        }else{
          that.setData({
            agree:false
           })
        }
        console.log(that.data)
      },
});